﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>Loop Reg - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The Loop Reg statement retrieves the contents of the specified registry subkey, one item at a time." />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
<style>
td.nowrap {
  white-space: nowrap
}
</style>
</head>
<body>

<h1>Loop Reg</h1>

<p>Retrieves the contents of the specified registry subkey, one item at a time.</p>

<pre class="Syntax"><span class="func">Loop Reg</span> KeyName <span class="optional">, Mode</span></pre>
<h2 id="Parameters">参数</h2>
<dl>

  <dt>KeyName</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>The full name of the registry key.</p>
    <p>This must start with HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, or HKEY_CURRENT_CONFIG (or the abbreviations for each of these, such as HKLM). To access a <a href="#remote">remote registry</a>, prepend the computer name and a slash, as in this example: \\workstation01\HKEY_LOCAL_MACHINE</p>
  </dd>
  
  <dt>Mode</dt>
  <dd>
    <p>类型: <a href="../Concepts.htm#strings">字符串</a></p>
    <p>零个或多个以下字母:</p>
    <p><code>K</code>: Include keys.<br>
    <code>V</code>: Include values. Values are also included if both K and V are omitted.<br>
    <code>R</code>: Recurse into subkeys. If R is omitted, keys and values within subkeys of <em>Key</em> are not included.</p>
  </dd>


</dl>

<h2 id="Remarks">备注</h2>
<p>A registry-loop is useful when you want to operate on a collection registry values or subkeys, one at a time. The values and subkeys are retrieved in reverse order (bottom to top) so that <a href="RegDelete.htm">RegDelete</a> and <a href="RegDeleteKey.htm">RegDeleteKey</a> can be used inside the loop without disrupting the loop.</p>
<p id="vars">The following variables exist within any registry-loop. If an inner registry-loop is enclosed by an outer registry-loop, the innermost loop's registry item will take precedence:</p>
<table class="info">
  <tr>
    <td>A_LoopRegName</td>
    <td>Name of the currently retrieved item, which can be either a value name or the name of a subkey. Value names displayed by Windows RegEdit as &quot;(Default)&quot; will be retrieved if a value has been assigned to them, but A_LoopRegName will be blank for them.</td>
  </tr>
  <tr>
    <td>A_LoopRegType</td>
    <td>The type of the currently retrieved item, which is one of the following words: KEY (i.e. the currently retrieved item is a subkey not a value), REG_SZ, REG_EXPAND_SZ, REG_MULTI_SZ, REG_DWORD, REG_QWORD, REG_BINARY, REG_LINK, REG_RESOURCE_LIST, REG_FULL_RESOURCE_DESCRIPTOR, REG_RESOURCE_REQUIREMENTS_LIST, REG_DWORD_BIG_ENDIAN (probably rare on most Windows hardware). It will be empty if the currently retrieved item is of an unknown type.</td>
  </tr>
  <tr>
    <td>A_LoopRegKey</td>
    <td>The full name of the key which contains the current loop item. For remote registry access, this value will <strong>not</strong> include the computer name.</td>
  </tr>
  <tr>
    <td>A_LoopRegTimeModified</td>
    <td>The time the current subkey or any of its values was last modified. Format <a href="FileSetTime.htm">YYYYMMDDHH24MISS</a>. This variable will be empty if the currently retrieved item is not a subkey (i.e. <em>A_LoopRegType</em> is not the word KEY).</td>
  </tr>
</table>
<p>When used inside a registry-loop, the following functions can be used in a simplified way to indicate that the currently retrieved item should be operated upon:</p>
<table class="info">
  <tr>
    <td class="nowrap"><code>Value := <a href="RegRead.htm">RegRead</a>()</code></td>
    <td>Reads the current item. If the current item is a key, 则抛出异常.</td>
  </tr>
  <tr>
    <td class="nowrap"><code><a href="RegWrite.htm">RegWrite</a> Value</code><br><code><a href="RegWrite.htm">RegWrite</a></code></td>
    <td>Writes to the current item. If <em>Value</em> is omitted, the item will be made 0 or blank depending on its type. If the current item is a key, an exception is thrown and the registry is not modified.</td>
  </tr>
  <tr>
    <td class="nowrap"><code><a href="RegDelete.htm">RegDelete</a></code></td>
    <td>Deletes the current item if it is a value. If the current item is a key, its default value will be deleted instead.</td>
  </tr>
  <tr>
    <td class="nowrap"><code><a href="RegDeleteKey.htm">RegDeleteKey</a></code></td>
    <td>Deletes the current item if it is a key. If the current item is a value, the key which <em>contains</em> that value will be deleted, including all subkeys and values.</td>
  </tr>
</table>
<p id="remote">When accessing a remote registry (via the <em>KeyName</em> parameter described above), the following notes apply:</p>
<ul>
  <li>The target machine must be running the Remote Registry service.</li>
  <li>Access to a remote registry may fail if the target computer is not in the same domain as yours or the local or remote username  lacks sufficient permissions (however, 请参阅 below for possible workarounds).</li>
  <li>Depending on your username's domain, workgroup, and/or permissions, you may have to connect to a shared device, such as by mapping a drive, prior to attempting remote registry access. Making such a connection -- using a remote username and password that has permission to access or edit the registry -- may as a side-effect enable remote registry access.</li>
  <li>If you're already connected to the target computer as a different user (例如, a mapped drive via user Guest), you may have to terminate that connection to allow the remote registry feature to reconnect and re-authenticate you as your own currently logged-on username.</li>
</ul>
<p>请参阅 <a href="Loop.htm">Loop</a> 了解关于<a href="Block.htm">区块</a>, <a href="Break.htm">Break</a>, <a href="Continue.htm">Continue</a> 和 A_Index 变量(其存在于各种类型的循环中) 的相关信息.</p>
<h2 id="Related">相关</h2>
<p><a href="Loop.htm">Loop</a>, <a href="Break.htm">Break</a>, <a href="Continue.htm">Continue</a>, <a href="Block.htm">Blocks</a>, <a href="RegRead.htm">RegRead</a>, <a href="RegWrite.htm">RegWrite</a>, <a href="RegDelete.htm">RegDelete</a>, <a href="RegDeleteKey.htm">RegDeleteKey</a>, <a href="SetRegView.htm">SetRegView</a></p>

<h2 id="Examples">示例</h2>
<div class="ex" id="ExRegDelete">
<p><a href="#ExRegDelete">#1</a>: Delete Internet Explorer's history of URLs typed by the user:</p>
<pre>Loop Reg, "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\TypedURLs"
    RegDelete</pre>
</div>

<div class="ex" id="ExBasic">
<p><a href="#ExBasic">#2</a>: A working test script:</p>
<pre>Loop Reg, "HKCU\Software\Microsoft\Windows", "R KV"  <em>; Recursively retrieve keys and values.</em>
{
    if a_LoopRegType = &quot;key&quot;
        value := &quot;&quot;
    else
    {
        try
            value := RegRead()
        catch
            value := &quot;*error*&quot;
    }
    Result := MsgBox(a_LoopRegName " = " value " (" a_LoopRegType ")`n`nContinue?",, "y/n")
}
Until Result = "No"</pre>
</div>

<div class="ex" id="ExRegSearch">
<p><a href="#ExRegSearch">#3</a>: A working example to recursively search the entire registry for particular value(s).</p>
<pre>
RegSearch("Notepad")

RegSearch(Target)
{
    Loop Reg, "HKEY_LOCAL_MACHINE", "KVR"
    {
        if !CheckThisRegItem()  <em>; It told us to stop.</em>
            return
    }
    Loop Reg, "HKEY_USERS", "KVR"
    {
        if !CheckThisRegItem()  <em>; It told us to stop.</em>
            return
    }
    Loop Reg, "HKEY_CURRENT_CONFIG", "KVR"
    {
        if !CheckThisRegItem()  <em>; It told us to stop.</em>
            return
    }
    <em>; Note: I believe HKEY_CURRENT_USER does not need to be searched if HKEY_USERS is
    ; being searched.  Similarly, HKEY_CLASSES_ROOT provides a combined view of keys from
    ; HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER, so searching all three isn't necessary.</em>

    CheckThisRegItem()
    {
        if A_LoopRegType = "KEY"  <em>; Remove these two lines if you want to check key names too.</em>
            return true
        try
            RegValue := RegRead()
        catch
            return true
        if InStr(RegValue, Target)
        {
            Result := MsgBox(
            (
            "The following match was found:
            " A_LoopRegKey "\" A_LoopRegName "
            Value = " RegValue "
            
            Continue?"
            ),, "y/n")
            if Result = "No"
                return false  <em>; Tell our caller to stop searching.</em>
        }
        return true
    }
}
</pre>
</div>

</body>
</html>